/**
 * Created with JetBrains PhpStorm.
 * User: power_yin
 * Date: 13-9-14
 * Time: 下午2:03
 * class AnimateNode
 */
(function(xf){
    /**
     * 节点动画 依赖 jQuery
     */
    "use strict";
    xf.AnimateNode = (function(){
        function AnimateNode(Slider_jQ){
            this.slider = Slider_jQ;
            this.style = {
                startColor : Slider_jQ.data('start-color'),
                endColor : Slider_jQ.data('end-color'),
                startLeft : Slider_jQ.data('start-left'),
                endLeft : Slider_jQ.data('end-left'),
                startTop : Slider_jQ.data('start-top'),
                endTop : Slider_jQ.data('end-top'),
                startOpacity : Slider_jQ.data('start-opacity'),
                endOpacity : Slider_jQ.data('end-opacity')
            };
            //初始化 节点
            this.init();
        }
        AnimateNode.prototype.init = function(){
            this.resume();
        };
        AnimateNode.prototype.resume = function(){
            this.slider.css({
                'left' : this.style.startLeft,
                'top' : this.style.startTop,
                'opacity' : this.style.startOpacity
            });
            this.slider.find('a').css({
                'color': this.style.startColor
            });
        };
        AnimateNode.prototype.animateStart = function(animateEndCallback){
            this.slider.animate({
                'left' : this.style.endLeft,
                'top' : this.style.endTop,
                'opacity' : this.style.endOpacity
            }, 1000, 'swing', animateEndCallback);
            this.slider.find('a').animate({
                'color': this.style.endColor
            }, 1000, 'swing');
        };
        return AnimateNode;
    })();
    /**
     * 文本动画
     * container 容器 id
     * animateNodeSelector 文本动画选择器 数组
     * class TextAnimate
     */
    xf.TextAnimate = ( function( $ ) {
        function TextAnimate( wrapperSelector ){
            this.wrapperSelector = wrapperSelector;
            this.sliderNodes = this._init();
            this.NodeStack = new XF.Node2Stack();		//创建节点堆栈
        }
        /**
         * 下一个方法
         */
        /*
        TextAnimate.prototype._nextMover = function( current_jQ ){

            var animateSet = {
                "title" : new XF.AnimateNode( current_jQ.find('div.hero-unit-part div.title') ),
                "content" : new XF.AnimateNode( current_jQ.find('div.hero-unit-part div.content') ),
                "linkBtn" : new XF.AnimateNode( current_jQ.find('div.hero-unit-part div.animate-btn') )
            };

            // 推入 节点 堆栈
            this.NodeStack.push(animateSet);

            //////// 内容和标题 动画开始  ////////
            animateSet.title.animateStart(function(){
                // title 完成之后
            });

            animateSet.content.animateStart(function(){
                // content 完成之后
            });
            animateSet.linkBtn.animateStart(function(){
                // linkBtn 完成之后
            });
            //////// 内容和标题 动画结束  ////////
        };
        */
        TextAnimate.prototype.nextMover = function( current_jQ ){
            var movers = {};
            current_jQ.find('.hero-unit-part').children().each(function(index, item){
                var item_jQ = $(item);
                movers[ item_jQ.data('unique-id') ] = new XF.AnimateNode( item_jQ );
            });
            this.NodeStack.push(movers);
            $.each(movers, function(index, move){
                move.animateStart(function(){
                    //完成之后
                });
            });
        };
        /**
         * 初始化方法
         * @private
         */
        TextAnimate.prototype._init = function() {
            var nodes = [];
            $( this.wrapperSelector ).find('.mc-image .slider-content').each(function(index, slider){
                nodes.push(slider);
            });
            return nodes;
        };

        return TextAnimate;
    })( jQuery );

})( XF || ( XF = {} )  );
/**
 * Created with JetBrains PhpStorm.
 * User: power_yin
 * Date: 13-9-14
 * Time: 下午2:03
 * class PopWinodw
 */
(function(xf, $){
    "use strict";
    xf.PopWinodw = (function(){
        function PopWinodw(windowSelector, btnPanelSelector){
            this.windowSelector = windowSelector;
            this.btnPanelSelector = btnPanelSelector;
            this.node2Stack = new XF.Node2Stack();
            this.nodes = this._initNodes();  //初始化节点
            this._bindEventToNodes();  //给节点绑定事件
        }
        PopWinodw.prototype._initNodes = function(){
            var Nodes = {};
            $(this.windowSelector + ' li').each(function(index, oLi){
                var jQ_oLi = $(oLi);
                var parentId = jQ_oLi.data('parent-id');
                Nodes[parentId] = new XF.PopNode(oLi);
            });
            return Nodes;
        };
        PopWinodw.prototype._bindEventToNodes = function(){
            var $this = this;
            $(this.btnPanelSelector + ' li').bind('mouseover', function(event){
                var currentNode = $this.nodes[ $(this).attr('id') ];
                $this.node2Stack.push({
                   "node" : currentNode
                });
                currentNode.show();
            }).bind('mouseout', function(event){
                var currentNode = $this.nodes[ $(this).attr('id') ];
                    currentNode.startHide();
            });

            $(this.windowSelector + ' li').bind('mouseover', function(event){
                var currentNode = $this.nodes[ $(this).data('parent-id') ];
                    currentNode.show();
            }).bind('mouseout', function(){
                var currentNode = $this.nodes[ $(this).data('parent-id') ];
                    currentNode.startHide();
            });
        };
        return PopWinodw;
    })();

    xf.PopNode = (function(){
        function PopNode( node ){
            this.jQInstance = $(node);
            this.parentId = this.jQInstance.data('parent-id');
            this.mutex = false; // 为延迟 关闭 的互斥变量
            this.instance = node;
            this._timerId = 0;
        }
        // 实现 Node2Stack 需要实现的 resume 方法
        PopNode.prototype.resume = function(){
            //立即消失
            window.clearTimeout( this._timerId );
            this.mutex = false;
            this.jQInstance.hide();
        };
        // 实现 Pop 的 show 方法
        PopNode.prototype.show = function(){
            this.jQInstance.show();
            this.mutex = true;
        };
        // 实现 hide 互斥变量 的 hide 方法
        PopNode.prototype.startHide = function(){
            this.mutex = false;
            this.registerCloseAction();
        };
        PopNode.prototype.registerCloseAction = function(){
            var $this = this;
            this._timerId = window.setTimeout(function(){
                if($this.mutex == false){
                    $this.jQInstance.hide();
                }else{
                    //继续 调用 自己 直至 this.mutex 为 false
                    $this.registerCloseAction();
                }
            }, 1000);
        };
        /**
         * 关联按钮 hover 动作 样式 改变
         */
        PopNode.prototype.relationBtnOverStyle = function(){
            $('#' + this.parentId + 'li a').removeClass('hover');
        };
        return PopNode;
    })();
})( XF || (XF = {}), jQuery );

